<?php 
	include($_SERVER['DOCUMENT_ROOT']."/headerins.php");
?>
<?php

try {
    //Open database connection


    //Getting records (listAction)
    if ($_GET["action"] == "list") {
        //Get records from database
        $result = mysql_query("
            SELECT 
            Id,
            Number, Email, Password, LastName, Name, 
            MiddleName, CreateDT, LastЕnterDT, 
            Active, ParentNumber
            FROM Users;");

        //Add all records to an array
        $rows = array();
        while ($row = mysql_fetch_array($result)) {
            $rows[] = $row;
        }

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Records'] = $rows;
        print json_encode($jTableResult);
    }
    //Creating a new record (createAction)
    else if ($_GET["action"] == "create") {
        //Insert record into database
		$bool = $_GET["active"];
		if($bool == "on")
			$bool = '1';
		else
			$bool = '0';

        $result = mysql_query("
            INSERT INTO material(
                Code, Name, GroupId, DimensionId, Price1, Price2, Active, Ball
            )VALUES(
                '" . $_GET["code"] . "', 
                '" . $_GET["name"] . "',
                '" . $_GET["matgroup"] . "',                    
                '" . $_GET["matdimension"] . "',
                '" . str_replace(',', '.', $_GET["price1"]). "',
                '" . str_replace(',', '.', $_GET["price2"]) . "',
                '" . $bool . "',
                '" . str_replace(',', '.', $_GET["ball"]) . "'
            );");

        if (!$result) {
            die('Ошибка при внесении записи: ' . mysql_error());
        }else
            print('Запись успешно добавлена');


    }else if ($_GET["action"] == "update") {
        //Update record in database
        $result = mysql_query("UPDATE material SET
		    code        = '" . $_GET["code"] . "'
            ,name        = '" . $_GET["name"] . "'
            ,groupid        = '" . $_GET["matgroup"] . "'
			,dimensionid  = '" . $_GET["matdimension"] . "'
			,price1			= '" . str_replace(',', '.', $_GET["price1"]) . "'
			,price2			 = '" . str_replace(',', '.', $_GET["price2"]) . "'
            ,ball		     = '" . str_replace(',', '.', $_GET["ball"]) . "'
            ,active        = '" . $_GET["active"] . "'
            WHERE id = " . $_GET["id"] . ";");
            
        if (!$result) {
            die('Ошибка при изменении записи: ' . mysql_error());
        }else
            print('Запись успешно изменена');
    }
    //Deleting a record (deleteAction)
    else if ($_GET["action"] == "delete") {
        //Delete from database
        $result = mysql_query("DELETE FROM material WHERE Id = " . $_GET["id"] . ";");
        
        if (!$result) {
            die('Ошибка при изменении записи: ' . mysql_error());
        }else
            print('Запись успешно удалена');
    }

    //Close database connection
    mysql_close($con);
} catch (Exception $ex) {
    //Return error message
//    $jTableResult = array();
//    $jTableResult['Result'] = "ERROR";
//    $jTableResult['Message'] = $ex->getMessage();
//    print json_encode($jTableResult);
    print($ex->getMessage());  
}
?>
